<?php

require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardRegister/lib/BasesfGuardRegisterActions.class.php');

class sfGuardRegisterActions extends BasesfGuardRegisterActions
{
  public function executeIndex(sfWebRequest $request)
  {
    if ($this->getUser()->isAuthenticated())
    {
      $msg = $this->getContext()->getI18N()->__('You are already registered and signed in!', null, 'action');
      $this->getUser()->setFlash('notice', $msg);
      $this->redirect('@homepage');
    }

    $class = sfConfig::get('app_sf_guard_plugin_register_form', 'sfGuardRegisterForm');
    $this->form = new $class();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter($this->form->getName()));
      if ($this->form->isValid())
      {
        $user = $this->form->save();
        $this->getUser()->signIn($user);

        $msg = $this->getContext()->getI18N()->__('Great, you are now registered and connected!', null, 'action');
        $this->getUser()->setFlash('notice', $msg);
        
        $this->redirect('trader_edit', $user->getTrader());
      }
    }
  }
}
